package chat.client;

import java.io.*;

import chat.clientToServerMessage.TextToServerMessage;

/**
 * 
 * @author Marius Eichenberg
 * started by a Client: only one WriteThread for a Client
 * this Thread waits for an input of a user
 * send this input to the ObjectOuptubSteam
 */
public class WriteThread extends Thread {
    
	private ObjectOutputStream o;
	private InputStreamReader isr = new InputStreamReader(System.in);
    private BufferedReader br = new BufferedReader(isr);
    private Client client;
    
    
  /**
   * constructor creates WriteThread to communicate via a ObjectOuptStream  
   * @param o
   */
  WriteThread(Client client, ObjectOutputStream o){
  this.o = o;
  this.client = client;
    }

  
  /**
   * run - Method of Thread to run the Thread
   */
  public void run() {

    while(true){
      try{
        String text = br.readLine();

        
        if(text.equals("exit")){
            client.exit();
        }
        if(text.equals("change Room")){
        	client.changeRoom();
        }
        else{
            TextToServerMessage message = new TextToServerMessage(text);
            o.writeObject(message);
        }
      }catch(Exception e){System.out.println(e);}
    }
  }
}
